package com.chezai.client.dao;

import android.database.sqlite.SQLiteDatabase;

import com.chezai.client.bean.ClassRecord;
import com.chezai.client.bean.CoachLoginRecord;
import com.chezai.client.bean.PeriodRecord;
import com.chezai.client.bean.RandomPhoto;
import com.chezai.client.bean.StudentStudyRecord;
import com.chezai.client.bean.UserInfo;
import com.chezai.client.dao.manager.TerminalParameterDaoManager;
import com.chezai.client.global.TerminalApplication;

import org.greenrobot.greendao.query.QueryBuilder;

/**
 * 数据库管理类
 */

public class DaoManager {

    private static DaoManager newInstance;
    //    private DaoMaster.DevOpenHelper mHelper;
    private MigrationOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession mDaoSession;

    private DaoManager() {
        QueryBuilder.LOG_SQL = false;
        QueryBuilder.LOG_VALUES = false;
        mHelper = new MigrationOpenHelper(TerminalApplication.getTerminalContext(), "terminal-db", null);
        db = mHelper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        mDaoSession = daoMaster.newSession();
    }

    public static DaoManager getInstance() {
        if (newInstance == null) {
            synchronized (DaoManager.class) {
                if (newInstance == null) {
                    newInstance = new DaoManager();
                }
            }
        }
        return newInstance;
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    /**
     * 删除使用数据
     */
    public void deleUseData() {
        mDaoSession.deleteAll(ClassRecord.class);
        mDaoSession.deleteAll(CoachLoginRecord.class);
        mDaoSession.deleteAll(PeriodRecord.class);
        mDaoSession.deleteAll(StudentStudyRecord.class);
        mDaoSession.deleteAll(UserInfo.class);
        mDaoSession.deleteAll(RandomPhoto.class);
        DaoMaster.createAllTables(daoMaster.getDatabase(), true);
    }

    /**
     * 清除全部数据
     */
    public void deleAllData() {
        DaoMaster.dropAllTables(daoMaster.getDatabase(), true);
        DaoMaster.createAllTables(daoMaster.getDatabase(), true);
        TerminalParameterDaoManager.getInstance().initTermianlParameter();
    }

}
